package com.toKnow.service.impl;

import com.toKnow.mapper.ArticleMapper;
import com.toKnow.mapper.ColumnMapper;
import com.toKnow.mapper.SearchHistoryMapper;
import com.toKnow.mapper.UserInfoMapper;
import com.toKnow.model.dto.SearchDto;
import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.ArticleAnswer;
import com.toKnow.model.entity.ArticleComment;
import com.toKnow.model.vo.RestrictionVo;
import com.toKnow.service.ArticleService;
import com.toKnow.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ColumnMapper columnMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public Article getArticle(Integer id) {
        if (id==null) return null;
        Article article = articleMapper.selectById(id);
        if (article==null) return null;
        article.setTagList(articleMapper.selectTagById(id));
        article.setTypeList(articleMapper.selectTypeById(id));
        return article;
    }

    @Override
    @Transactional
    public Boolean browseArticle(Integer userId, Integer articleId) {
        if (userId==null||articleId==null) return false;
        articleMapper.updateReadNumber(articleId,1);
        Article article = articleMapper.selectById(articleId);
        if (article==null) throw new ServiceException("没有找到文章");
        List<Integer> columnIdList = columnMapper.selectColumnByArticleId(articleId);
        for (Integer id : columnIdList){
            columnMapper.updateReadNumber(id,1);
            columnMapper.insertUserRead(userId,id);
        }
        Integer authorId=article.getAuthorId();
        userInfoMapper.updateReadTimes(authorId,1);
        return articleMapper.insertUserRead(userId, articleId);
    }

    @Override
    @Transactional
    public Boolean changeLikeArticle(Integer userId, Integer articleId) {
        if (userId==null||articleId==null) return false;
        List<Integer> columnIdList = columnMapper.selectColumnByArticleId(articleId);
        Article article = articleMapper.selectById(articleId);
        if (article==null) throw new ServiceException("没有找到文章");
        Integer authorId=article.getAuthorId();
        if (!isLikeArticle(userId, articleId)){
            if(!articleMapper.updateLikeNumber(articleId,1)){
                throw new ServiceException("没有找到文章");
            }
            for (Integer id : columnIdList){
                columnMapper.updateLikeNumber(id,1);
                userInfoMapper.updateLikeTimes(authorId,1);
            }
            return articleMapper.insertUserLike(userId, articleId);
        }else {
            if(!articleMapper.updateLikeNumber(articleId,-1)){
                throw new ServiceException("没有找到文章");
            }
            for (Integer id : columnIdList){
                columnMapper.updateLikeNumber(id,-1);
                userInfoMapper.updateLikeTimes(authorId,-1);
            }
            return articleMapper.deleteUserLike(userId, articleId);
        }
    }

    @Override
    @Transactional
    public Boolean changeCollectArticle(Integer userId, Integer articleId) {
        if (userId==null||articleId==null) return false;
        Article article = articleMapper.selectById(articleId);
        if (article==null) throw new ServiceException("没有找到文章");
        Integer authorId=article.getAuthorId();
        if (!isCollectArticle(userId, articleId)){
            if(!articleMapper.updateCollectNumber(articleId,1)){
                throw new ServiceException("没有找到文章");
            }
            userInfoMapper.updateCollectTimes(article.getAuthorId(),1);
            return articleMapper.insertUserCollect(userId, articleId);
        }else {
            if(!articleMapper.updateCollectNumber(articleId,-1)){
                throw new ServiceException("没有找到文章");
            }
            userInfoMapper.updateCollectTimes(article.getAuthorId(),-1);
            return articleMapper.deleteUserCollect(userId, articleId);
        }
    }

    @Override
    public Boolean isCollectArticle(Integer userId, Integer articleId) {
        if (userId==null||articleId==null) return false;
        return articleMapper.checkUserCollect(userId,articleId);
    }

    @Override
    public Boolean isLikeArticle(Integer userId, Integer articleId) {
        if (userId==null||articleId==null) return false;
        return articleMapper.checkUserLike(userId, articleId);
    }
    @Transactional(readOnly = false)
    @Override
    public List<Article> searchArticle(Integer uid,RestrictionVo restrictionVo) {
        String content=restrictionVo.getKeyword();
        if (content!=null&&!content.equals("")&&uid!=null)
            searchHistoryMapper.insertSearchHistory(uid,content);
        return articleMapper.selectArticleBySearch(new SearchDto(restrictionVo,"article"));
    }

    @Override
    public List<ArticleComment> getComment(Integer articleId, Integer page) {
        if (articleId==null) throw new ServiceException("没有携带文章编号");
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return articleMapper.selectArticleCommentByArticleId(articleId,(page-1)*10);
    }

    @Override
    public List<ArticleAnswer> getAnswer(Integer commentId, Integer page) {
        if (commentId==null) throw new ServiceException("没有携带评论编号");
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return articleMapper.selectArticleAnswerByCommentId(commentId,(page-1)*10);
    }

    @Override
    public List<Article> getDraftArticle(Integer uid) {
        return articleMapper.getDraftArticle(uid);
    }

    @Override
    public List<Article> getCheckingArticle(Integer uid) {
        return articleMapper.getCheckingArticle(uid);
    }

    @Override
    public Article selectArticleDraftById(Integer uid, Integer aid) {
        Article article = articleMapper.selectArticleDraftById(uid, aid);
        if(article == null) return article;
        article.setTagList(articleMapper.selectDraftTagById(aid));
        article.setTypeList(articleMapper.selectDraftTypeById(aid));
        return article;
    }
}
